import Axios from 'axios'
import store from '../stores/index'

var baseUrl = 'http://'

var axios = Axios.create({
  baseURL: baseUrl,
  timeout: '15000', // 请求超时时间
  headers: {
    // 'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Type': 'multipart/form-data',
  },
})

axios.interceptors.request.use(
  function (config) {
    // 在发送请求之前做某事
    let tokenStr = 'token ' + store.UserStore.token
    config.headers['Authorization'] = tokenStr
    return config
  },
  function (error) {
    // 请求错误时做些事
    return Promise.reject(error)
  }
)

// 添加响应拦截器
axios.interceptors.response.use(
  function (response) {
    store.UserStore.setOffLine(false)
    // 对响应数据做些事
    // if(response.data.Code === 0) {
    return response
    // }else if(response.data.Code === 1) {
    //   return response.Message;
    // }
  },
  function (error) {
    // 请求错误时做些事
    store.UserStore.setOffLine(true)
    let nowHref = window.location.href
    if (nowHref.indexOf('offline') > -1) {
    } else {
      // window.location.href = 'http://' +  nowHost + '/2019/allcity/#/offline'
    }
    return Promise.reject(error)
  }
)

export default axios
